package com.szf.system.web;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.szf.base.dao.Page;
import com.szf.base.exception.GenericException;
import com.szf.base.web.BaseActionSupport;
import com.szf.base.web.WebUtil;
import com.szf.system.entity.ComParam;
import com.szf.system.entity.ComParamType;
import com.szf.system.service.ParamService;
import com.szf.util.UuidGenerator;

@Namespace("/")
@Results({ @Result(name = "success", location = "/WEB-INF/jsp/system/com-param-type.jsp"), @Result(name = "input", location = "/WEB-INF/jsp/system/com-param-type-input.jsp"), @Result(name = "param", location = "/WEB-INF/jsp/system/com-param.jsp"), @Result(name = "param-input", location = "/WEB-INF/jsp/system/com-param-input.jsp") })
public class ParamAction extends BaseActionSupport {
	@Autowired
	private ParamService paramService;
	private ComParamType paramType;
	private ComParam comParam;

	@Override
	public String execute() {
		return super.execute();
	}

	public String goParam() {
		paramType = paramService.findParamTypeById(paramType.getTypeId());
		return "param";
	}

	public void listParamType() {
		Page page = paramService.queryByIbatis(warpRequest(request), "sinlog.listParamType");
		WebUtil.renderJson(response, wrapResult(page));
	}

	public void listParam() {
		Page page = paramService.queryByIbatis(warpRequest(request), "sinlog.listParam");
		WebUtil.renderJson(response, wrapResult(page));
	}

	public String add() {
		paramType = new ComParamType();
		paramType.setTypeId(UuidGenerator.getUuid());
		method = "add";
		return "input";
	}

	public String addParam() {
		comParam.setParamId(UuidGenerator.getUuid());
		method = "add";
		return "param-input";
	}

	public String edit() {
		paramType = paramService.findParamTypeById(paramType.getTypeId());
		method = "edit";
		return "input";
	}

	public String editParam() {
		comParam = paramService.findParamById(comParam.getParamId());
		method = "edit";
		return "param-input";
	}

	public String view() {
		paramType = paramService.findParamTypeById(paramType.getTypeId());
		method = "view";
		return "input";
	}

	public String viewParam() {
		comParam = paramService.findParamById(comParam.getParamId());
		method = "view";
		return "param-input";
	}

	public void update() {
		try {
			if ("add".equals(method)) {
				ComParamType tmp = paramService.findParamTypeByTypeCode(paramType.getTypeCode());
				if (tmp != null) {
					throw new GenericException("类型代码重复！");
				}
			}
			paramService.saveParamType(paramType);
			ajaxDefaultResponse();
		} catch (Exception e) {
			ajaxErrorResponse(e.getMessage());
			logger.error(e.getMessage(), e);
		}
	}

	public void updateParam() {
		try {
			if ("add".equals(method)) {
				ComParam tmp = paramService.findParamByParamValue(comParam.getParamValue(), comParam.getTypeId());
				if (tmp != null) {
					throw new GenericException("参数值重复！");
				}
			}
			paramService.saveParam(comParam);
			ajaxDefaultResponse();
		} catch (Exception e) {
			ajaxErrorResponse(e.getMessage());
			logger.error(e.getMessage(), e);
		}
	}

	public void delete() {
		try {
			String ids = request.getParameter("ids");
			paramService.deleteParamType(ids);
			ajaxDefaultResponse();
		} catch (Exception e) {
			ajaxErrorResponse(e.getMessage());
			logger.error(e.getMessage(), e);
		}
	}

	public void deleteParam() {
		try {
			String ids = request.getParameter("ids");
			paramService.deleteParam(ids);
			ajaxDefaultResponse();
		} catch (Exception e) {
			ajaxErrorResponse(e.getMessage());
			logger.error(e.getMessage(), e);
		}
	}

	public void refresh() {
		try {
			paramService.init();
			ajaxDefaultResponse();
		} catch (Exception e) {
			ajaxErrorResponse(e.getMessage());
			logger.error(e.getMessage(), e);
		}

	}

	public ComParamType getParamType() {
		return paramType;
	}

	public void setParamType(ComParamType paramType) {
		this.paramType = paramType;
	}

	public ComParam getComParam() {
		return comParam;
	}

	public void setComParam(ComParam comParam) {
		this.comParam = comParam;
	}

}
